from PIL import Image
from PIL import ImageFilter
from PIL import ImageEnhance
import numpy as np
import matplotlib.pyplot as plt
import os
import random
import json

z = 1
Picture_Name = 'f'
Picture_num = 10
Bird_low_num = 200
Bird_high_num = 300
Back_ground_path = "C:/Users/Peter/Desktop/back_ground/"
Bird_spices = "D:/Bird_Cont/Bird_sign/"  # D:/cropped_bird/000000000813/
for i in range(Picture_num):
    list1 = []
    rootdir = Back_ground_path
    file_names = []
    for parent, dirnames, filenames in os.walk(rootdir):
        file_names = filenames
    x = random.randint(0, len(file_names) - 1)
    im1 = Image.open(rootdir + file_names[x])
    if random.random() > 0.5 or 1:
        im1 = im1.transpose(Image.FLIP_LEFT_RIGHT)

    c = np.random.randint(low=Bird_low_num, high=Bird_high_num)
    print(c)
    for x in range(c):
        rootdir = Bird_spices
        file_names = []
        for parent, dirnames, filenames in os.walk(rootdir):
            file_names = filenames
        x = random.randint(0, len(file_names) - 1)
        im2 = Image.open(rootdir + file_names[x])

        if random.random() > 0.5 or 1:
            im2 = im2.transpose(Image.FLIP_LEFT_RIGHT)
        if im2.size[0] > 100 and im2.size[1] > 100:
            im2 = im2.resize((int(im2.size[0] / 10), int(im2.size[1] / 10)), Image.ANTIALIAS)
        a = np.random.randint(low=im1.size[0])
        b = np.random.randint(low=im1.size[1])
        s = (a, b)
        im1.paste(im2, s, im2)
        v = ((a + int(im2.size[0] / 2)), (b + int(im2.size[1] / 2)))
        # v = np.array(v)
        list1.append(v)

            # array = np.array(list1)
            # print(array)
    # print(len(list1))
    temp_dict = {}
    # result = []
    submit = 'D:/Bird_Cont/Bird_add/jsons/' + Picture_Name + str(z) + '.json'
    temp_dict['human_num'] = (len(list1))
    temp_dict['points'] = list1
    # result.append(temp_dict)
    with open(submit, 'w') as f:
        json.dump(temp_dict, f)
    im1.save('D:/Bird_Cont/Bird_add/images/' + Picture_Name + str(z) + '.jpg')
    z = z + 1
    # print(len(list1))
    # plt.imshow(im1)
    # plt.show()
